<template>
  <div class="mod-config">
      <el-form inline v-show="searchShow" ref="searchForm" :model="dataForm" label-width="80px" @keyup.enter.native="searchChange">
        <el-row>
          <el-col :span="8">
            <el-form-item label="代金券号">
              <el-input v-model="dataForm.vouchersNum"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="使用顾客">
              <el-input v-model="dataForm.customerName"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="使用日期">
              <el-row>
                <el-col :span="11">
                  <el-date-picker type="date" v-model="dataForm.useTimeStart" style="width: 100%;"></el-date-picker>
                </el-col>
                <el-col :span="2" style="text-align: center;">至</el-col>
                <el-col :span="11">
                  <el-date-picker type="date" v-model="dataForm.useTimeEnd" style="width: 100%;"></el-date-picker>
                </el-col>
              </el-row>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="消费类型">
              <el-checkbox-group v-model="dataForm.typeIds">
                <el-checkbox label="1" name="type">框镜</el-checkbox>
                <el-checkbox label="2" name="type">隐形</el-checkbox>
                <el-checkbox label="3" name="type">辅料</el-checkbox>
                <el-checkbox label="4" name="type">挂号</el-checkbox>
                <el-checkbox label="5" name="type">维修费</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="有效期">
              <el-row>
                <el-col :span="11">
                  <el-date-picker type="date" v-model="dataForm.validityStart" style="width: 100%;"></el-date-picker>
                </el-col>
                <el-col :span="2" style="text-align: center;">至</el-col>
                <el-col :span="11">
                  <el-date-picker type="date" v-model="dataForm.validityEnd" style="width: 100%;"></el-date-picker>
                </el-col>
              </el-row>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="代金券金额">
              <el-row>
                <el-col :span="11">
                  <el-input v-model="dataForm.moneyMin"></el-input>
                </el-col>
                <el-col :span="2" style="text-align: center;">至</el-col>
                <el-col :span="11">
                  <el-input v-model="dataForm.moneyMax"></el-input>
                </el-col>
              </el-row>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="searchChange">查询</el-button>
              <el-button type="default" icon="el-icon-delete" @click="searchReset">清空</el-button>
            </el-form-item>
        </el-row>
      </el-form>
      <el-row>
        <el-col :span="24">
          <el-button v-if="permissions.eims_vouchers_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle('','add')">新增</el-button>
          <el-button v-if="permissions.eims_vouchers_add" icon="el-icon-plus" type="primary" @click="addByBatch()">批量新增</el-button>
          <el-button icon="el-icon-search" circle @click="handleSearchShow" style="float:right;"></el-button>
          <el-button icon="el-icon-refresh" circle @click="refreshChange" style="float:right;"></el-button>
        </el-col>
      </el-row>
      <div class="avue-crud">
        <el-table :data="dataList" height="calc(100vh - 215px)" border v-loading="dataListLoading">
          <el-table-column label="操作" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-collection" @click="addOrUpdateHandle(scope.row.id,'detail')">详情</el-button>
              <el-button v-if="permissions.eims_vouchers_edit" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id,'update')">修改</el-button>
              <el-button v-if="permissions.eims_vouchers_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="vouchersNum" label="代金券号"></el-table-column>
          <el-table-column prop="money" label="代金券金额"></el-table-column>
          <el-table-column prop="validity" label="有效期"></el-table-column>
          <el-table-column prop="typeName" label="消费类型"></el-table-column>
          <el-table-column prop="customerName" label="使用顾客"></el-table-column>
          <el-table-column prop="useTime" label="使用时间"></el-table-column>
        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
        </el-pagination>
      </div>
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
      <add-batch v-if="batchVisible" ref="batchDialog" @refreshDataList="getDataList"></add-batch>
  </div>
</template>

<script>
  import { getVouchersPage, delVouchers } from '@/api/eims/vouchers'
  import { filterParams } from '@/util/tools'
  import TableForm from './components/AddOrUpdate'
  import AddBatch from './components/AddBatch'
  import { mapGetters } from 'vuex'
  export default {
    data() {
      return {
        dataForm: {
          vouchersNum: '',
          customerName: '',
          useTimeStart: '',
          useTimeEnd: '',
          typeIds: [],
          validityStart: '',
          validityEnd: '',
          moneyMin: '',
          moneyMax: ''
        },
        dataList: [],
        searchShow: true,
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        batchVisible: false
      }
    },
    components: {
      TableForm,
      AddBatch
    },
    created() {
      this.getDataList()
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    methods: {
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        let params1 = { current: this.pageIndex, size: this.pageSize }
        let params2 = filterParams(this.dataForm)
        getVouchersPage(Object.assign(params1,params2)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle(id,type) {
        this.addOrUpdateVisible = true
        let data = {
          id: id,
          type: type
        }
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(data)
        })
      },
      // 删除
      deleteHandle(id) {
        this.$confirm('是否确认删除ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delVouchers(id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('删除成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //  批量新增
      addByBatch() {
        this.batchVisible = true
        this.$nextTick(() => {
          this.$refs.batchDialog.init()
        })
      },
      searchChange() {
        this.pageIndex = 1;
        this.getDataList()
      },
      // 搜索清空
      searchReset() {
        for(let key in this.dataForm){
          if(key == 'typeIds'){
            this.dataForm.typeIds = []
          }else{
            this.dataForm[key] = ''
          }
        }
      },
      handleSearchShow() {
        this.searchShow = !this.searchShow;
      },
      // 刷新回调
      refreshChange() {
        this.getDataList(this.page)
      }
    }
  }
</script>
